package cn.tedu.yushouge.pet.adopt.controller;

import cn.tedu.yushouge.commons.pojo.vo.PageData;
import cn.tedu.yushouge.pet.adopt.pojo.param.PetAddNewParam;
import cn.tedu.yushouge.pet.adopt.pojo.param.PetAddNewParamOfString;
import cn.tedu.yushouge.pet.adopt.pojo.param.PetUpdateInfoParam;
import cn.tedu.yushouge.pet.adopt.pojo.vo.PetListItemVO;
import cn.tedu.yushouge.pet.adopt.pojo.vo.PetStandardVO;
import cn.tedu.yushouge.pet.adopt.service.IPetService;
import cn.tedu.yushouge.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDate;

@RestController
@RequestMapping("/pet")
@Api(tags = "02. 宠物管理模块")
@Slf4j
@Validated
public class PetController {

    @Autowired
    IPetService petService;

    //http://localhost:9818/pet/add-new
    @PostMapping("/add-new")
    @ApiOperation("添加宠物")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid PetAddNewParamOfString petAddNewParamOfString) {
        log.debug("开始处理【添加宠物】的请求，参数：{}", petAddNewParamOfString);
        petService.addNew(petAddNewParamOfString);
        return JsonResult.ok();
    }
    //http://localhost:9818/pet/delete
    @PostMapping("/delete")
    @ApiOperation("根据ID删除宠物")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "宠物ID", required = true, dataType = "long")
    })
    public JsonResult<Void> delete(@Range(min = 1, message = "根据ID删除宠物失败，请提交合法的ID值！")
                             @RequestParam Long id) {
        log.debug("开始处理【根据ID删除宠物】的请求，参数：{}", id);
        petService.deleteById(id);
        return JsonResult.ok();
    }

    //http://localhost:9818/pet/update
    @PostMapping("/update")
    @ApiOperation("修改宠物详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "品牌ID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfoById(@RequestParam @Range(min = 1, message = "请提交有效的宠物ID值!") Long id,
                                     @Valid PetUpdateInfoParam petUpdateInfoParam) {
        log.debug("开始处理【修改宠物详情】的业务，参数：{}",petUpdateInfoParam);
        petService.updateInfoById(id,petUpdateInfoParam);
        return JsonResult.ok();
    }

    //http://localhost:9818/pet/standard
    @GetMapping("/standard")
    @ApiOperation("根据ID查询宠物详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单ID", required = true, dataType = "long")
    })
    public JsonResult<PetStandardVO> getStandardById(@Range(min = 1, message = "根据ID查询宠物详情失败，请提交合法的ID值！")
                                      @RequestParam Long id) throws Exception {
        log.debug("开始处理【根据ID查询宠物详情】的请求，参数：{}", id);
        PetStandardVO result = petService.getStandardById(id);
        return JsonResult.ok(result);
    }

    //http://localhost:9818/pet/list
    @GetMapping("/list")
    @ApiOperation("查询宠物")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query")
    })
    public JsonResult<PageData<PetListItemVO>> list(@Range(min = 1, message = "查询宠物失败，请提供正确的页码值！") Integer pageNum) {
        log.debug("开始处理【查询宠物列表】的请求，页码：{}", pageNum);
        if (pageNum == null || pageNum < 1) {
            pageNum = 1;
        }
        PageData<PetListItemVO> pageData = petService.list(pageNum);
        return JsonResult.ok(pageData);
    }

}
